﻿namespace Agen
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Agen.LoginUI;
    using System.Diagnostics;
    using Agen.Models;
    using System;
    using System.Windows.Shapes;
    using System.Windows.Media;


    /// <summary>
    /// <see cref="UserControl"/> class providing the main UI for the application.
    /// </summary>
    public partial class MainPage : UserControl
    {
        /// <summary>
        /// Creates a new <see cref="MainPage"/> instance.
        /// </summary>
        public MainPage()
        {
            DataContext = ApplicationManager.CURRENT_EMPLOYEE;
            InitializeComponent();
            ApplicationManager.FrameActionEventHandler += new System.EventHandler<FrameEventArgs>(ApplicationManager_FrameActionEventHandler);
        }

        void ApplicationManager_FrameActionEventHandler(object sender, FrameEventArgs e)
        {
            Debug.WriteLine("DBG : ApplicationManager_FrameActionEventHandler =>  FrameEventArgs = " + e.ToString());
            this.ContentFrame.Navigate(new Uri(ApplicationManager.CURRENT_URI, UriKind.Relative));
            updateUI();
        }

        /// <summary>
        /// If an error occurs during navigation, show an error window
        /// </summary>
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ErrorWindow.CreateNew(e.Exception);
        }

        private void updateUI()
        {
            if (ApplicationManager.CURRENT_URI != ApplicationManager.URI_LOGIN && ApplicationManager.CURRENT_EMPLOYEE == null)
            {
                ApplicationManager.FrameAction(this, ApplicationManager.NO_EMPLOYEE_HANDLE, new Object());
            }

            if (ApplicationManager.CURRENT_URI != ApplicationManager.URI_LOGIN)
            {
                deconnection_link.Visibility = Visibility.Visible;
                
                firstName.Text = ApplicationManager.CURRENT_EMPLOYEE.Person.FirstName;
                lastName.Text = ApplicationManager.CURRENT_EMPLOYEE.Person.LastName;
                navigation_container.Children.Clear();
                foreach(string element in ApplicationManager.applicationNavigation){

                    HyperlinkButton link = new HyperlinkButton();
                    link.Click+=new RoutedEventHandler(link_Click);
                    link.Content = ApplicationManager.URI_LABEL[element];
                    link.Name = element;
                    link.VerticalAlignment = VerticalAlignment.Center;
                    navigation_container.Children.Add(link);
                    Rectangle separator = new Rectangle();
                    separator.Width = 2;
                    separator.Height = 21;
                    separator.Fill = new SolidColorBrush(Colors.White);
                    navigation_container.Children.Add(separator);
                }
            }
            else {
                cleanApp();
            }
        }

        void link_Click(object sender, RoutedEventArgs e)
        {
            ApplicationManager.FrameAction(this, ApplicationManager.URI_EVENT[(sender as HyperlinkButton).Name], new Object());
        }

        private void navigation_link_Click(object sender, RoutedEventArgs e)
        {
            ApplicationManager.FrameAction(this, ApplicationManager.PREVIOUS_HANDLE, new Object());
        }

        private void cleanApp() {
            deconnection_link.Visibility = Visibility.Collapsed;
            firstName.Text = "";
            lastName.Text = "";
            navigation_container.Children.Clear();     
        }

        private void deconnection_link_Click(object sender, RoutedEventArgs e)
        {
            ApplicationManager.FrameAction(this, ApplicationManager.NO_EMPLOYEE_HANDLE, new Object());
            cleanApp();
        }
    }
}